home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Flock 1.0 beta / flock-1.0RC3.en-US.win32.exe / flock / components / flockSearchAPITechnorati.js < prev    next >
Text File  |  2007-10-18  |  10KB  |  246 lines

  1. //
  2. // BEGIN FLOCK GPL
  3. // 
  4. // Copyright Flock Inc. 2005-2007
  5. // http://flock.com
  6. // 
  7. // This file may be used under the terms of of the
  8. // GNU General Public License Version 2 or later (the "GPL"),
  9. // http://www.gnu.org/licenses/gpl.html
  10. // 
  11. // Software distributed under the License is distributed on an "AS IS" basis,
  12. // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. // for the specific language governing rights and limitations under the
  14. // License.
  15. // 
  16. // END FLOCK GPL
  17. //
  18.  
  19. const TECHNORATI_SEARCH_CID         = Components.ID('{88a8b490-d044-4a3b-bb33-1798863ffabf}');
  20.  
  21. const nsISupports                   = Components.interfaces.nsISupports;
  22. const nsIClassInfo                  = Components.interfaces.nsIClassInfo;
  23. const nsIFactory                    = Components.interfaces.nsIFactory;
  24. const nsIProperties                 = Components.interfaces.nsIProperties;
  25. const nsILocalFile                  = Components.interfaces.nsILocalFile;
  26. const nsIFile                       = Components.interfaces.nsIFile;
  27. const nsIIOService                  = Components.interfaces.nsIIOService;
  28. const nsIFileProtocolHandler        = Components.interfaces.nsIFileProtocolHandler;
  29. const nsIRDFRemoteDataSource        = Components.interfaces.nsIRDFRemoteDataSource;
  30. const nsIRDFDataSource              = Components.interfaces.nsIRDFDataSource;
  31. const flockISearchService           = Components.interfaces.flockISearchService;
  32. const nsIXMLHttpRequest             = Components.interfaces.nsIXMLHttpRequest;
  33.  
  34. const TECHNORATI_SEARCH_CONTRACTID       = '@flock.com/?flock-search-technorati;1';
  35. const DIRECTORY_SERVICE_CONTRACTID  = '@mozilla.org/file/directory_service;1';
  36. const LOCAL_FILE_CONTRACTID         = '@mozilla.org/file/local;1';
  37. const PREFERENCES_CONTRACTID        = '@mozilla.org/preferences-service;1';
  38. const IO_SERVICE_CONTRACTID         = '@mozilla.org/network/io-service;1';
  39. const XMLHTTPREQUEST_CONTRACTID     = '@mozilla.org/xmlextras/xmlhttprequest;1'
  40.  
  41. const flockIError = Components.interfaces.flockIError;
  42. const FLOCK_ERROR_CONTRACTID = '@flock.com/error;1'
  43.  
  44. const TECHNORATI_SEARCH_API_URL = "http://api.technorati.com/search";
  45. const TECHNORATI_API_KEY = "2dd47dbb72b25cc7130a450255af779b";
  46.  
  47. function technoratiSearchService() {
  48.   var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
  49.   var bundle = sbs.createBundle("chrome://flock/locale/search/search.properties");
  50.   this.serviceName = bundle.GetStringFromName("flock.search.api.technorati");
  51. }
  52.  
  53. technoratiSearchService.prototype.shortName = "technorati";
  54. technoratiSearchService.prototype.icon = "http://technorati.com/favicon.ico";
  55. technoratiSearchService.prototype.ref = "urn:flock:search:technorati";
  56. technoratiSearchService.prototype.fullResultsUrl = "http://technorati.com/search/";
  57.  
  58. technoratiSearchService.prototype.search = function (aQuery, aNumResults, aListener, aDatasource) {
  59.     var inst = this;
  60.     
  61.     var url = TECHNORATI_SEARCH_API_URL + "?" 
  62.        + "key=" + TECHNORATI_API_KEY
  63.        + "&query=" + aQuery
  64.        + "&format=xml"
  65.        + "&start=1&limit=" + aNumResults;
  66.  
  67.     debug(url + " < call\n");
  68.     this.req = Components.classes[XMLHTTPREQUEST_CONTRACTID].createInstance(nsIXMLHttpRequest);
  69.     this.req instanceof Components.interfaces.nsIJSXMLHttpRequest;
  70.     this.req.open('GET', url, true);
  71.     var req = this.req;    
  72.     //debug('request is ' + req + '\n');
  73.     this.req.onreadystatechange = function (aEvt) {
  74.       if(inst.req.readyState == 4) {
  75.           // debug("\nRESPONSE\n" + inst.xmlhttp.responseText);
  76.           try {
  77.               if(req.status == 200 || req.status == 201) {
  78.                  try {
  79.                      //processor(listener, inst);
  80.                      var rdf = inst.readXML(req.responseXML, aDatasource);
  81.                      var numResults = inst.getNumResults(req.responseXML);
  82.                      aListener.foundResults(numResults, rdf, inst.shortName, aQuery);
  83.                  } catch(e) {
  84.                      debug(e + " " + e.lineNumber+"\n");
  85.                  }
  86.               }
  87.               else {
  88.                   var faultString = req.responseText;
  89.                   // listener.onFault(faultString);
  90.               }
  91.           } catch(e) {
  92.               debug(e + " " + e.fileName + " " + e.lineNumber + "\n");
  93.               //listener.onError(inst.ERROR_PARSER);
  94.           }
  95.       }
  96.     }
  97.     
  98.     this.req.send(null);  
  99. }
  100.  
  101. technoratiSearchService.prototype.getNumResults = function (xmlDoc) {
  102.     try {
  103.         var results = xmlDoc.getElementsByTagName("item"); 
  104.         return results.length;
  105.     } catch (ex) {
  106.     
  107.     }
  108. }
  109.  
  110. technoratiSearchService.prototype.readXML = function (xmlDoc, aDatasource) {
  111.   try {
  112.     var resultSet = xmlDoc.getElementsByTagName("ResultSet")[0];                                                              
  113.     var totalResultsAvailable = parseInt(xmlDoc.getElementsByTagName("querycount")[0].firstChild.nodeValue);
  114.     var totalResultsReturned = parseInt(xmlDoc.getElementsByTagName("item").length);                                      
  115.     //var firstResultPosition = parseInt(resultSet.getAttribute("firstResultPosition"));                                        
  116.     //var start = firstResultPosition;                                                                                          
  117.     //var end = firstResultPosition + totalResultsReturned - 1;                                                                 
  118.     var results = xmlDoc.getElementsByTagName("weblog"); 
  119.  
  120.     // Create an in-memory datasource
  121.     var rdfService = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  122.         .getService(Components.interfaces.nsIRDFService);
  123.     //var rdf = Components.classes["@mozilla.org/rdf/datasource;1?name=in-memory-datasource"].createInstance(Components.interfaces.nsIRDFDataSource);
  124.     var rdfContainerUtils = Components.classes["@mozilla.org/rdf/container-utils;1"]
  125.         .getService(Components.interfaces.nsIRDFContainerUtils);    
  126.         
  127.     var rootNode = rdfService.GetResource("urn:flock:search:technorati");
  128.     
  129.     // clear any existing results
  130.     var props=aDatasource.ArcLabelsOut(rootNode);
  131.     while(props.hasMoreElements()){
  132.         var prop=props.getNext();
  133.         var target=aDatasource.GetTarget(rootNode,prop,true);
  134.         try {
  135.           //target=target.QueryInterface(Components.interfaces.nsIRDFResource);
  136.           aDatasource.Unassert(rootNode,prop,target)
  137.         }
  138.         catch (e){
  139.             debug ('yahoo error clearing the ds ' + e + '\n');
  140.         }
  141.     }    
  142.     
  143.     rootNode = rdfService.GetResource("urn:flock:search:technorati");
  144.     var container = rdfContainerUtils.MakeSeq(aDatasource, rootNode);
  145.     for (var i = 0; i < results.length; i++) { 
  146.         var result = results[i];                                                                                                
  147.         var title = result.getElementsByTagName('name')[0].firstChild.nodeValue;
  148.         var clickUrl = result.getElementsByTagName('url')[0].firstChild.nodeValue;
  149.         var url = result.getElementsByTagName('url')[0].firstChild.nodeValue;
  150.         
  151.         var subject = rdfService.GetResource(url);
  152.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#Name");
  153.         var name = rdfService.GetLiteral(title);                
  154.         aDatasource.Assert(subject, predicate, name, true);
  155.  
  156.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#URL");
  157.         var name = rdfService.GetLiteral(url);
  158.         aDatasource.Assert(subject, predicate, name, true);
  159.         
  160.         var predicate = rdfService.GetResource("http://home.netscape.com/NC-rdf#favicon");
  161.         var name = rdfService.GetLiteral(this.icon);
  162.         aDatasource.Assert(subject, predicate, name, true);
  163.  
  164.         container.AppendElement(subject);
  165.     }
  166.  
  167.     return aDatasource;
  168.   } catch(ex) {
  169.     debug('technoratiSearchService ' + ex + '\n');
  170.   }
  171. }
  172.  
  173.  
  174. technoratiSearchService.prototype.flags = nsIClassInfo.SINGLETON;
  175. technoratiSearchService.prototype.classDescription = "Technorati Search Service";
  176. technoratiSearchService.prototype.getInterfaces = function (count) {
  177.     var interfaceList = [flockISearchService, nsIClassInfo];
  178.     count.value = interfaceList.length;
  179.     return interfaceList;
  180. }
  181. technoratiSearchService.prototype.getHelperForLanguage = function (count) {return null;}
  182.  
  183. // the nsISupports implementation
  184. technoratiSearchService.prototype.QueryInterface =
  185. function (iid) {
  186.     if (!iid.equals(flockISearchService) && 
  187.         !iid.equals(nsIClassInfo) &&
  188.         !iid.equals(nsISupports))
  189.         throw Components.results.NS_ERROR_NO_INTERFACE;
  190.     if (iid.equals(nsIRDFDataSource) && !this.dataSourceSetup) {
  191.     }
  192.     return this;
  193. }
  194.  
  195. // Module implementation
  196. var FlockSearchModule = new Object();
  197.  
  198. FlockSearchModule.registerSelf =
  199. function (compMgr, fileSpec, location, type)
  200. {
  201.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  202.  
  203.     compMgr.registerFactoryLocation(TECHNORATI_SEARCH_CID, 
  204.                                     "Technorati Search JS Component",
  205.                                     TECHNORATI_SEARCH_CONTRACTID, 
  206.                                     fileSpec, 
  207.                                     location,
  208.                                     type);
  209.     //necessary category registration
  210.     var catmgr = Components.classes["@mozilla.org/categorymanager;1"]
  211.         .getService (Components.interfaces.nsICategoryManager);
  212.     catmgr.addCategoryEntry('flockISearchService', 'technorati', TECHNORATI_SEARCH_CONTRACTID, true, true);
  213. }
  214.  
  215. FlockSearchModule.getClassObject =
  216. function (compMgr, cid, iid) {
  217.     if (!cid.equals(TECHNORATI_SEARCH_CID))
  218.         throw Components.results.NS_ERROR_NO_INTERFACE;
  219.     
  220.     if (!iid.equals(Components.interfaces.nsIFactory))
  221.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  222.     
  223.     return SeachServiceFactory;
  224. }
  225.  
  226. FlockSearchModule.canUnload =
  227. function(compMgr)
  228. {
  229.     return true;
  230. }
  231.     
  232. /* factory object */
  233. var SeachServiceFactory = new Object();
  234.  
  235. SeachServiceFactory.createInstance =
  236. function (outer, iid) {
  237.     if (outer != null)
  238.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  239.     return (new technoratiSearchService()).QueryInterface(iid);
  240. }
  241.  
  242. /* entrypoint */
  243. function NSGetModule(compMgr, fileSpec) {
  244.     return FlockSearchModule;
  245. }
  246.